import {
	GetALLImgInfo,
	GetIndexAllACtircle,
	GetALLViews,
	AllViews,
	LookTitleDetail,
	Praise,
	login,
	logout
} from '@/api/article.js'
import {
	setToken,
	getToken,
	removeTken,
	setUserinfo,
	removeUserinfo,
	getUserinfo
} from '@/util/Token.js'
const state = {
	codeurl: '', // 图形验证码
	success: '', // 验证标识符
	userinfo: getUserinfo() || {},
	actircleinfo: [], //文章内容
	newParseNumber: '', //新的点赞数量
	token: getToken() || '',
	AllIndexImg: null,
	detailActricleInfo: {}

}
const mutations = {
	//设置用户信息
	SETUSERINFO(state, userinfo) {
		state.userinfo = userinfo
	},
	//设置文章
	GETALLACTICLE(state, info) {
		state.actircleinfo = info
	},
	//设置点赞数量
	GETGOPARSENUMBER(state, number) {
		state.newParseNumber = number
	},
	//保存token
	SETTOKEN(state, token) {
		state.token = token
	},
	//设置首页轮播图
	SETINDEXIMG(state, imgData) {
		state.AllIndexImg = imgData
	},
	//拿到详情文章
	GETDETAILACTRICLE(state, detailInfo) {
		state.detailActricleInfo = detailInfo
	}

}
const actions = {
	// 登录操作
	async Login(context, userInfo) {
		const {
			phone,
			password,
			code,
			uuid
		} = userInfo
		const res = await login(code, password, phone, uuid)
		// console.log(res)
		if (res.data.code == 0) {
			//保存token
			const accessToken = res.data.result.accessToken
			context.commit('SETTOKEN', accessToken)
			//保存到本地
			setToken(accessToken)
			const newres = res.data.result
			// 保存用户信息
			context.commit('SETUSERINFO', newres)
			setUserinfo(newres)
			return new Promise((resolve, reject) => {
				resolve('ok')
			})
		} else {
			return new Promise((reslove, reject) => {
				reject('fail')
			})
		}


	},

	//退出登录
	async logout(context) {
		// 转化id类型为number
		// console.log(context.state.userinfo.userId)
		const id = parseInt(context.state.userinfo.userId)

		let result = await logout(id)
		if (result.data.code === 0) {
			removeTken()
			removeUserinfo()
		}
	},

	// 	//拿到所有的图片资源
	async getallImg(context) {
		let res = await GetALLImgInfo()
		if (res.data.code === 0) {
			context.commit('SETINDEXIMG', res.data.result);
			// console.log(res);
			return new Promise((resolve, reject) => {
				resolve('ok')
			})

		} else {
			return new Promise((reslove, reject) => {
				reject('fail');
			})
		}

	},
	//首页根据根据文章进行分类
	async GetindexAllInfo(context, aDataInfo) {
		const {
			category_id,
			current,
			orderByTime,
			size,
			title
		} = aDataInfo
		let res = await GetIndexAllACtircle({
			category_id,
			current,
			orderByTime,
			size,
			title
		})
		if (res.data.code === 0) {
			console.log('首页数据', res)
			context.commit('GETALLACTICLE', res.data.result.list)
			return 'ok'
		}
	},
	// 	//获取浏览量
	async getAllViews(context, Articleid) {
		let res = await GetALLViews(Articleid)
	},
	// 浏览量增加
	async AddViews(context, Articleid) {
		console.log(Articleid)
		let res = await AllViews(Articleid)
		if (res.data.code === 0) {
			return 'ok'
		}
		// console.log(res)

	},
	// 	//根据文章id查询文章
	async LookTitleDetail(context, id) {
		let res = await LookTitleDetail(id)
		if (res.data.code == 0) {
			context.commit('GETDETAILACTRICLE', res.data.result)
			return 'ok'
		}

	},
	// 	//点赞并返回数量
	async GoPraise(context, ACinfo) {
		const {
			Articleid,
			status
		} = ACinfo
		let res = await Praise(Articleid, status)
		if (res.statusCode === 200) {
			context.commit('GETGOPARSENUMBER', res.data.result)
			return 'ok'
		}
	}
}
// const getters = {}
export default {
	state,
	mutations,
	actions,
	// 	getters
}
